using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {
    public class PreviewSelector : VisualElement {
        private readonly PreviewEnv previewEnv;

        private const string nullChoice = "-";

        private PopupField<AGEPreviewPlayer> playersField;
        private Button refreshPlayersListButton;
        private Button connectButton;
        private Button disconnectButton;

        private string selectName;
        public PreviewSelector(PreviewEnv previewEnv) {
            this.previewEnv = previewEnv;
            style.flexDirection = FlexDirection.Row;
            BuildUI();
            DrawUI();
        }

        void DrawUI() {
            if (previewEnv.isConnecting) {
                playersField.SetEnabled(false);
                Add(playersField);
                Add(disconnectButton);
            } else {
                playersField.SetEnabled(true);
                Add(playersField);
                Add(refreshPlayersListButton);
                Add(connectButton);
            }
        }

        void BuildUI() {
            playersField = new PopupField<AGEPreviewPlayer>() {
                style = {
                    // flexGrow = 1,
                }
            };
            playersField.RegisterValueChangedCallback(evt => {
                if (evt.newValue == null) {
                    return;
                }
                var newName = evt.newValue.gameObject.name;
                if (newName == selectName) {
                    return;
                } else {
                    selectName = newName;
                }
                //
            });
            playersField.formatListItemCallback = ShowPopupText; // 决定每个选项的文本
            playersField.formatSelectedValueCallback = ShowPopupText; // 决定显示在下拉框中的文本

            refreshPlayersListButton = new Button();
            refreshPlayersListButton.text = "refresh";
            refreshPlayersListButton.clicked += RefreshListButton;

            connectButton = new Button();
            connectButton.text = "connect";
            connectButton.clicked += Connect;

            disconnectButton = new Button();
            disconnectButton.text = "disconnect";
            disconnectButton.clicked += Disconnect;

            RefreshListButton();
        }

        string ShowPopupText(AGEPreviewPlayer p) {
            if (p == null) {
                return nullChoice;
            } else if (p.isHolding) {
                return "(Holding) " + p.gameObject.name;
            } else {
                return p.gameObject.name;
            }
        }

        void RefreshListButton() {
            var agePreview = AGEGlobal.GetInstance().preview;
            agePreview.GetPlayersInScene();
            var players = agePreview.GetPreviewPlayerList();
            players.Insert(0, null);
            playersField.choices = players;
            playersField.index = 0;
            if (selectName != null) {
                for (int i = 1; i < players.Count; i++) {
                    if (selectName == players[i].gameObject.name) {
                        playersField.index = i;
                    }
                }
            }
        }
        private void Connect() {
            if (selectName != null) {
                previewEnv.Connect(selectName);
            }
            Clear();
            DrawUI();
        }
        private void Disconnect() {
            previewEnv.Disconnect();
            Clear();
            DrawUI();
        }

    }
}
